package cn.wolfcode.shop.cloud.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class CookieUtil {
    public static void addCookie(String cookieName, String cookieValue, int exprieTime,
                                 HttpServletResponse response){
        Cookie cookie = new Cookie(cookieName,cookieValue);
        cookie.setDomain("localhost");//设置作用域
        cookie.setPath("/");//设置路径
        cookie.setMaxAge(exprieTime);
        response.addCookie(cookie);
    }

    public static String getCookieVal(HttpServletRequest request, String cookieName) {
       //获取所有的cookie
        Cookie[] cookies = request.getCookies();
        if (cookies!=null&&cookies.length>0){
            for (Cookie cookie : cookies) {
                if (cookieName.equals(cookie.getName())){
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

   
}
